/**
 * @author mapeng
 * @version 2017-05-09
 */

(function(doc, win) {
  var pageVariables = {
    loadFromCache: false,
    shopId: "",
    type: "",
    handleBack4WeixinOpenStep1: "",
    handleForward: "",
    handleBack: ""
  };

  $(doc).on("pageInit", "#weixinOpenStep1Page", function(
    e,
    pageId,
    $page,
    isBack,
    isForward
  ) {
    if (isForward) return;

    if (isBack) {
      handlePageBack();
      return;
    }

    init4Page();
  });
  bindEvents4Page();

  //页面返回
  function handlePageBack() {
    var params = CommonUtil.takeOutParamObject() || {};
    pageVariables.handleForward = params.handleForward || "load";

    params.handleForward = "";
    CommonUtil.injectParamObject(params);
  }

  //前进
  function doForward() {
    if (pageVariables.loadFromCache == true) {
      $.router.forward();
    } else {
      $.router.load("weixin_open_step2.html", true); // ignore cache
    }
  }

  //页面进入
  function handlePageLoad() {
    var params = CommonUtil.takeOutParamObject() || {};
    pageVariables.handleBack = params.handleBack || "back";
    params.handleBack = "";
    CommonUtil.injectParamObject(params);
  }

  //返回
  function doBack() {
    if (pageVariables.handleBack == "back") {
      $.router.back();
    } else if (pageVariables.handleBack == "load") {
      var cache = CommonUtil.takeOutParamObject() || {};
      cache.shopId = pageVariables.shopId;
      cache.handleBack = "load";
      CommonUtil.injectParamObject(cache);
      $.router.load("store_detail.html", true);
    }
  }

  //处理返回进入
  function handleFromBack() {
    var params = CommonUtil.takeOutParamObject() || {};
    if (params.backFrom == "weixinOpenStep1Page") {
      pageVariables.loadFromCache = true;
    } else {
      pageVariables.loadFromCache = false;
    }

    params.backFrom = "";
    CommonUtil.injectParamObject(params);
  }

  //页面初始化
  function init4Page() {
    //页面参数
    var params = CommonUtil.takeOutParamObject() || {};
    pageVariables.shopId = params.shopId;
    pageVariables.type = params.type;
    handlePageLoad();

    //账户类型
    init4AccountType();

    //行业分类
    init4IndustryType();

    //经营类目
    init4ManagementCategory();

    //     //门店所在地区
    //    init4ShopDistrict();
  }

  //账户类型
  function init4AccountType() {
    var accountTypeUl = "<ul style='display:none;'>";
    $.each(Constants.weixin.ACCOUNT_TYPE, function(key, value) {
      accountTypeUl +=
        "<li data-code='" +
        value +
        "' data-val='" +
        Constants.weixin.getAccountTypeText(value) +
        "'>" +
        Constants.weixin.getAccountTypeText(value) +
        "</li>";
    });
    accountTypeUl += "</ul>";
    $("#weixinOpenStep1Page div[name='accountType']").prepend(accountTypeUl);

    $("#weixinOpenStep1Page div[name='accountType']")
      .children("ul")
      .mobiscroll()
      .treelist({
        theme: "ios",
        display: "bottom",
        lang: "zh",
        placeholder: "请选择账户",
        onSelect: function(valueText, inst) {
          var codeArray = [];
          var $ul = ($this = $(this));
          $.each(valueText.split("-"), function(index, value) {
            var $li = $ul.children("li[data-val='" + value + "']");
            codeArray.push($li.attr("data-code"));
            $ul = $li.children("ul").eq(0);
          });
          $this
            .parent()
            .find("input[type='hidden']")
            .val(codeArray.join(","));

          //字段切换
          setAccoutFields(codeArray[0]);

          listen4NextStep();
        }
      });
    //默认“个人账户”
    $("#weixinOpenStep1Page div[name='accountType']")
      .children("ul")
      .mobiscroll(
        "setVal",
        Constants.weixin.getAccountTypeText(
          Constants.weixin.ACCOUNT_TYPE.person
        ),
        true
      );
    $("#weixinOpenStep1Page div[name='accountType'] input[type='hidden']").val(
      Constants.weixin.ACCOUNT_TYPE.person
    );
    setAccoutFields(Constants.weixin.ACCOUNT_TYPE.person);
  }
  //账户类型字段切换
  function setAccoutFields(accountType) {
    if (accountType == Constants.weixin.ACCOUNT_TYPE.person) {
      $("#weixinOpenStep1Page .person").css("display", "");
      $("#weixinOpenStep1Page .company").css("display", "none");
    } else if (accountType == Constants.weixin.ACCOUNT_TYPE.company) {
      $("#weixinOpenStep1Page .person").css("display", "none");
      $("#weixinOpenStep1Page .company").css("display", "");
    }
  }

  //初始化行业分类
  function init4IndustryType() {
    var industryTypeUl = "<ul style='display:none;'>";
    $.each(Constants.weixin.INDUSTRY_TYPE, function(key, value) {
      industryTypeUl +=
        "<li data-code='" +
        value +
        "' data-val='" +
        Constants.weixin.getIndustryTypeText(value) +
        "'>" +
        Constants.weixin.getIndustryTypeText(value) +
        "</li>";
    });
    industryTypeUl += "</ul>";
    $("#weixinOpenStep1Page div[name='industryType']").prepend(industryTypeUl);

    $("#weixinOpenStep1Page div[name='industryType']")
      .children("ul")
      .mobiscroll()
      .treelist({
        theme: "ios",
        display: "bottom",
        lang: "zh",
        placeholder: "请选择商户类型",
        onSelect: function(valueText, inst) {
          var $ul = $(this);
          var $li = $ul.children("li[data-val='" + valueText + "']");
          var code = $li.attr("data-code");
          $ul
            .parent()
            .find("input[type='hidden']")
            .val(code);

          listen4NextStep();
        }
      });
  }
  //经营类目
  function init4ManagementCategory() {
    var dict = {
      "2015050700000000": "美食",
      "2015091000052157": "超市便利店",
      "2015062600004525": "休闲娱乐",
      "2015062600002758": "购物",
      "2016062900190124": "爱车",
      "2015063000020189": "生活服务",
      "2016042200000148": "教育培训",
      "2016062900190296": "医疗健康",
      "2015080600000001": "航旅",
      "2016062900190337": "专业销售/批发",
      "2016062900190371": "政府/社会组织"
    };
    var categoryUl = "<ul style='display:none;'>";
    $.each(dict, function(key, value) {
      categoryUl +=
        "<li data-code='" + key + "' data-val='" + key + "'>" + value + "</li>";
    });
    categoryUl += "</ul>";
    $("#weixinOpenStep1Page div[name='manageCategory']").prepend(categoryUl);

    $("#weixinOpenStep1Page div[name='manageCategory']")
      .children("ul")
      .mobiscroll()
      .treelist({
        theme: "ios",
        display: "bottom",
        lang: "zh",
        placeholder: "请选择经营类目",
        onSelect: function(valueText, inst) {
          var $ul = $(this);
          var $li = $ul.children("li[data-val='" + valueText + "']");
          var code = $li.attr("data-code");
          $ul
            .parent()
            .find("input[type='hidden']")
            .val(code);

          listen4NextStep();
        }
      });
  }

  //初始化门店地区
  function init4ShopDistrict() {
    common.loading();
    CityUtil.getProviceCityAreaHtml(function(ul) {
      common.closeLoading();
      $("#weixinOpenStep1Page div[name='shopDistrict']").prepend(ul);
      $("#weixinOpenStep1Page div[name='shopDistrict']")
        .children("ul")
        .mobiscroll()
        .treelist({
          theme: "ios",
          display: "bottom",
          lang: "zh",
          placeholder: "请选择省、市、区(县)",
          formatValue: function(data) {
            return data.join("-");
          },
          onSelect: function(valueText, inst) {
            var codeArray = [];
            var $ul = ($this = $(this));
            $.each(valueText.split("-"), function(index, value) {
              var $li = $ul.children("li[data-val='" + value + "']");
              codeArray.push($li.attr("data-code"));
              $ul = $li.children("ul").eq(0);
            });
            $this
              .parent()
              .find("input[type='hidden']")
              .val(codeArray.join(","));

            listen4NextStep();
          }
        });
    });
  }

  //校验返回
  function confirm4Back() {
    var shopName = $("#weixinOpenStep1Page input[name='shopName']")
      .val()
      .trim();
    var shortShopName = $("#weixinOpenStep1Page input[name='shortShopName']")
      .val()
      .trim();
    var industryType = $(
      "#weixinOpenStep1Page div[name='industryType'] input[type='hidden']"
    ).val();
    //经营类目
    var manageCategory = $(
      "#weixinOpenStep1Page div[name='manageCategory'] input[type='hidden']"
    ).val();
    //var shopDistrict = $("#weixinOpenStep1Page div[name='shopDistrict'] input[type='hidden']").val();
    //var address = $("#weixinOpenStep1Page input[name='address']").val().trim();
    var bossName = $("#weixinOpenStep1Page input[name='bossName']")
      .val()
      .trim();
    var businessRegistrationNum = $(
      "#weixinOpenStep1Page input[name='businessRegistrationNum']"
    ).val();
    var businessPhoto = $("#weixinOpenStep1Page li[name='businessPhoto']").attr(
      "weixinImgUrl"
    );
    var orgPhoto = $("#weixinOpenStep1Page li[name='orgPhoto']").attr(
      "weixinImgUrl"
    );
    var doorheadPhoto = $("#weixinOpenStep1Page li[name='doorheadPhoto']").attr(
      "weixinImgUrl"
    );
    var yzBusinessPhoto = $(
      "#weixinOpenStep1Page li[name='businessPhoto']"
    ).attr("yzImgUrl");
    var yzOrgPhoto = $("#weixinOpenStep1Page li[name='orgPhoto']").attr(
      "yzImgUrl"
    );
    var yzDoorheadPhoto = $(
      "#weixinOpenStep1Page li[name='doorheadPhoto']"
    ).attr("yzImgUrl");

    //没有填写任何表单
    if (
      !shopName &&
      !shortShopName &&
      !industryType &&
      !manageCategory &&
      !bossName &&
      !businessRegistrationNum &&
      !businessPhoto &&
      !orgPhoto &&
      !doorheadPhoto &&
      !yzBusinessPhoto &&
      !yzOrgPhoto &&
      !yzDoorheadPhoto
    ) {
      doBack();
      return;
    }

    //已填写：需要确认是否放弃
    DialogUtil.dialogConfirm({
      title: "",
      content: "离开页面将不会保存本次修改，确定要离开吗？",
      btnLeft: "取消",
      btnRight: "确定",
      callbackLeft: function() {},
      callbackRight: function() {
        doBack();
      }
    });
  }

  //控制“下一步”是否可点击
  function listen4NextStep() {
    //====新验证逻辑
    var flag = false;
    $.each($("#weixinOpenStep1Page .input-required"), function() {
      var val = $(this).val();
      if (val === null || val == "") {
        flag = true;
        return false;
      }
    });                                                                       
    if (flag) {
      $("#weixinOpenStep1Page .header li.right").removeClass("disabled");
    } else {
      $("#weixinOpenStep1Page .header li.right").addClass("disabled");
    }
    return false;
    // //====以下为部分旧逻辑太麻烦了
    // var shopName = $("#weixinOpenStep1Page input[name='shopName']")
    //   .val()
    //   .trim();
    // var shortShopName = $("#weixinOpenStep1Page input[name='shortShopName']")
    //   .val()
    //   .trim();
    // var industryType = $(
    //   "#weixinOpenStep1Page div[name='industryType'] input[type='hidden']"
    // ).val();
    // var manageCategory = $(
    //   "#weixinOpenStep1Page div[name='manageCategory'] input[type='hidden']"
    // ).val();
    // //var shopDistrict = $("#weixinOpenStep1Page div[name='shopDistrict'] input[type='hidden']").val();
    // //var address = $("#weixinOpenStep1Page input[name='address']").val();
    // var bossName = $("#weixinOpenStep1Page input[name='bossName']").val();
    // var businessRegistrationNum = $(
    //   "#weixinOpenStep1Page input[name='businessRegistrationNum']"
    // ).val();
    // var businessPhoto = $("#weixinOpenStep1Page li[name='businessPhoto']").attr(
    //   "weixinImgUrl"
    // );
    // var orgPhoto = $("#weixinOpenStep1Page li[name='orgPhoto']").attr(
    //   "weixinImgUrl"
    // );
    // var doorheadPhoto = $("#weixinOpenStep1Page li[name='doorheadPhoto']").attr(
    //   "weixinImgUrl"
    // );
    // var yzBusinessPhoto = $(
    //   "#weixinOpenStep1Page li[name='businessPhoto']"
    // ).attr("yzImgUrl");
    // var yzOrgPhoto = $("#weixinOpenStep1Page li[name='orgPhoto']").attr(
    //   "yzImgUrl"
    // );
    // var yzDoorheadPhoto = $(
    //   "#weixinOpenStep1Page li[name='doorheadPhoto']"
    // ).attr("yzImgUrl");
    // // $("#weixinOpenStep1Page .header li.right").removeClass("disabled");
    // // return;
    // if (
    //   shopName &&
    //   shortShopName &&
    //   industryType &&
    //   manageCategory &&
    //   bossName &&
    //   businessRegistrationNum &&
    //   businessPhoto &&
    //   orgPhoto &&
    //   doorheadPhoto &&
    //   yzBusinessPhoto &&
    //   yzOrgPhoto &&
    //   yzDoorheadPhoto
    // ) {
    //   $("#weixinOpenStep1Page .header li.right").removeClass("disabled");
    //   return;
    // }

    // $("#weixinOpenStep1Page .header li.right").addClass("disabled");
  }

  //下一步
  function next() {
   
    //表单数据
   var shopName = $("#weixinOpenStep1Page input[name='shopName']")
      .val()
      .trim();  
    var shortShopName = $("#weixinOpenStep1Page input[name='shortShopName']")
      .val()
      .trim();
    var industryType = $(
      "#weixinOpenStep1Page div[name='industryType'] input[type='hidden']"
    ).val();
    var industryTypeText = $(
      "#weixinOpenStep1Page div[name='industryType']>ul"
    ).mobiscroll("getVal");
    var manageCategory = $(
      "#weixinOpenStep1Page div[name='manageCategory'] input[type='hidden']"
    ).val();                                                                      
    //var shopDistrict = $("#weixinOpenStep1Page div[name='shopDistrict'] input[type='hidden']").val();
    // var shopDistrictText = $(
    //   "#weixinOpenStep1Page div[name='shopDistrict']>ul"
    // ).mobiscroll("getArrayVal");
    //var address = $("#weixinOpenStep1Page input[name='address']").val().trim();
    // var bossName = $("#weixinOpenStep1Page input[name='bossName']").val().trim();                     
    //将第二步中的身份证信息拿过来
  
    var mobile = $("#weixinOpenStep1Page input[name='principalMobile']").val().trim(); //手机号
    var yzImgUrl4Front = $("#weixinOpenStep1Page li[name='frontPicture']").attr(
      "yzImgUrl"
    ); //正面：云纵
    var weixinImgUrl4Front = $(
      "#weixinOpenStep1Page li[name='frontPicture']"
    ).attr("weixinImgUrl"); //正面：微信
    var yzImgUrl4Back = $("#weixinOpenStep1Page li[name='backPicture']").attr(
      "yzImgUrl"
    ); //反面：云纵
    var weixinImgUrl4Back = $(
      "#weixinOpenStep1Page li[name='backPicture']"
    ).attr("weixinImgUrl"); //反面：微信

    var businessRegistrationNum = $(
      "#weixinOpenStep1Page input[name='businessRegistrationNum']"
    ).val();
    var businessPhoto = $("#weixinOpenStep1Page li[name='businessPhoto']").attr(
      "weixinImgUrl"
    );
    var orgPhoto = $("#weixinOpenStep1Page li[name='orgPhoto']").attr(
      "weixinImgUrl"
    );
    var doorheadPhoto = $("#weixinOpenStep1Page li[name='doorheadPhoto']").attr(
      "weixinImgUrl"
    );
    var yzBusinessPhoto = $(
      "#weixinOpenStep1Page li[name='businessPhoto']"
    ).attr("yzImgUrl");
    var yzOrgPhoto = $("#weixinOpenStep1Page li[name='orgPhoto']").attr(
      "yzImgUrl"
    );
    var yzDoorheadPhoto = $(
      "#weixinOpenStep1Page li[name='doorheadPhoto']"
    ).attr("yzImgUrl");

    //校验：门店名称
    if (shopName.length == 0) {
      DialogUtil.dialogWarn("请填写门店名称");
      return;
    }
    if (!/^[\dA-Za-z\u4e00-\u9fa5]+$/.test(shopName)) {
      DialogUtil.dialogWarn("门店名称仅允许输入中文、英文、数字");
      return;
    }
    if (CommonUtil.countBytes(shopName, 2) > 64) {
      DialogUtil.dialogWarn("门店名称输入过长");
      return;
    }

    //校验：简称
    if (shortShopName.length == 0) {
      DialogUtil.dialogWarn("请填写简称");
      return;
    }
    if (!/^[\dA-Za-z\u4e00-\u9fa5]+$/.test(shortShopName)) {
      DialogUtil.dialogWarn("简称仅允许输入中文、英文、数字");
      return;
    }
    if (CommonUtil.countBytes(shortShopName, 2) > 30) {
      DialogUtil.dialogWarn("简称输入过长");
      return;
    }

    //校验：商户类型
    if (!industryType) {
      DialogUtil.dialogWarn("请选择商户类型");
      return;
    }
    //校验：经营类目
    if (!manageCategory) {
      DialogUtil.dialogWarn("请选择经营类目");
      return;
    }                                                                                            
                                                                                    
    // //校验：所在地区
    // if(!shopDistrict){
    // 	DialogUtil.dialogWarn("请选择所在地区");
    // 	return;
    // }

    // //校验：详细地址
    // if(address.length == 0){
    // 	DialogUtil.dialogWarn("请填写详细地址");
    // 	return;
    // }
    // if (!/^[\dA-Za-z\u4e00-\u9fa5]+$/.test(address)) {
    //   DialogUtil.dialogWarn("详细地址仅允许输入中文、英文、数字");
    //   return;
    // }
    // if(CommonUtil.countBytes(address,2) > 128){
    // 	DialogUtil.dialogWarn("详细地址输入过长");
    // 	return;
    // }

    // //校验：企业法人
    // if (bossName.length == 0) {
    //   DialogUtil.dialogWarn("请填写企业法人");
    //   return;
    // }
    // if (!/^[\dA-Za-z\u4e00-\u9fa5]+$/.test(bossName)) {
    //   DialogUtil.dialogWarn("企业法人仅允许输入中文、英文、数字");
    //   return;
    // }
    // if (CommonUtil.countBytes(bossName, 2) > 128) {
    //   DialogUtil.dialogWarn("企业法人输入过长");
    //   return;
    // } 
    //校验：手机号
    if (mobile.length == 0) {
      DialogUtil.dialogWarn("请填写手机号");
      return;
    }
    if (!/^[1][3578][0-9]{9}$/.test(mobile)) {
      DialogUtil.dialogWarn("请填写有效的手机号");
      return;
    }
    // 校验手机号是否在黑名单内
    CommonUtil.sendJsonpRequest({
		"url" : App.serverUrl+"/app/wxAccount/verifyMobile.do",
    "async":false,
		"params" : {"mobile":mobile},
		"success" : function(result){
			if(result.code == -1){//校验不通过  
		  	DialogUtil.dialogWarn(result.msg);
        
			}else{
          //保存本页数据
    var cache = CommonUtil.takeOutParamObject() || {};
    cache.shopId = pageVariables.shopId;
    cache.type = pageVariables.type;
    cache.stepOne = {
      merchantName: shopName, //门店名称
      merchantShortName: shortShopName, //简称
      industrId: industryType, //行业类别
      industrName: industryTypeText,
      //   province: shopDistrict.split(",")[0], //省
      //   city: shopDistrict.split(",")[1], //市
      //   county: shopDistrict.split(",")[2], //区
      //   provinceName: shopDistrictText[0],
      //   cityName: shopDistrictText[1],
      //   countyName: shopDistrictText[2],
      //"address" : address , //详细地址
      //   legalPeople: bossName, //企业法人
      businessRegistrationNum: businessRegistrationNum, //营业执照工商注册号
      orgPhoto: orgPhoto, //组织机构照
      businessPhoto: businessPhoto, //营业执照
      doorheadPhoto: doorheadPhoto, //门头照
      yzOrgPhoto: yzOrgPhoto, //云纵组织机构照
      yzBusinessPhoto: yzBusinessPhoto, //云纵营业执照
      yzDoorheadPhoto: yzDoorheadPhoto //云纵门头照
      };
    CommonUtil.injectParamObject(cache);

    //跳转
    doForward();
    
      }
		},
		"error" : function(){//not 200
			DialogUtil.dialogWarn("校验手机号失败,请检查网络稍后请再尝试");
      
		},
   	});
   
  }

  //页面事件绑定
  function bindEvents4Page() {
    //返回
    $(document).on("click", "#weixinOpenStep1Page .header a.back", function(e) {
      confirm4Back();
    });

    //监听输入，控制下一步是否可点击
    var listenFormStr =
      "#weixinOpenStep1Page input[name='shopName']," +
      "#weixinOpenStep1Page input[name='shortShopName']," +
      "#weixinOpenStep1Page input[name='servicePhoneNo']," +
      "#weixinOpenStep1Page input[name='principal']," +
      "#weixinOpenStep1Page input[name='principalMobile']," +
      "#weixinOpenStep1Page input[name='idCard']";
    $(document).on("input", listenFormStr, function(e) {
      listen4NextStep();
    });
    $(document).on("clearInput", "#weixinOpenStep1Page .zhk-list_gb", function(
      e
    ) {
      listen4NextStep();
    });
    //营业执照
    $(document).on("click", "#weixinOpenStep1Page input[type='file']", function(
      e
    ) {
      //阻止file默认行为
      e.preventDefault();
    });
    $(document).on("click", "#weixinOpenStep1Page li.picture", function(e) {
      //上传
      if ($(e.target).hasClass("zhk-list_gb")) return;

      uploadPicture($(this));
    });
    $(document).on(
      "click",
      "#weixinOpenStep1Page .picture .zhk-list_gb",
      function(e) {
        //删除
        var $li = $(this).parents("li.picture");
        displayPicture($li, "hide");

        $li.attr("yzImgUrl", "");
        $li.attr("weixinImgUrl", "");

        listen4NextStep();
      }
    );

    //下一步
    $(document).on(
      "click",
      "#weixinOpenStep1Page .header a[name='nextStep']",
      function(e) {
        if (
          $(this)
            .parents("li.right")
            .hasClass("disabled")
        )
          return;

        next();
      }
    );
  }

  //上传身份证照片
  function uploadPicture(target) {
    $.actions([
      [
        {
          text: "相册",
          onClick: function() {
            //选择
            AppUtil.call(
              AppUtil.APIS.SELECT_PICTURE,
              {
                compress: { sizeLimit: "500", unit: "K" },
                format: "JPG",
                maxNum: "1"
              },
              function(selectResult) {
                if (selectResult.success.toString() == "true") {
                  var filePath = selectResult.resultObject[0];

                  //上传
                  common.loading("上传中");
                  AppUtil.call(
                    AppUtil.APIS.UPLOAD_PICTURE,
                    {
                      url: App.serverUrl + "/app/img/upload.do",
                      file: filePath,
                      data: {
                        shopId: pageVariables.shopId,
                        uploadType: target.attr("uploadType")
                      }
                    },
                    function(uploadResult) {
                      common.closeLoading();
                      if (
                        uploadResult.success.toString() == "true" &&
                        uploadResult.resultObject.success.toString() == "true"
                      ) {
                        target.attr(
                          "yzImgUrl",
                          uploadResult.resultObject.resultObject.imageUrl
                        );
                        target.attr(
                          "weixinImgUrl",
                          uploadResult.resultObject.resultObject
                            .thirdPartyImageUrl
                        );

                        //显示图片
                        target.find("img").attr("src", filePath);
                        displayPicture(target, "show");

                        listen4NextStep();
                      } else {
                        var msg = "";
                        if (uploadResult.success.toString() == "false") {
                          msg = uploadResult.msg;
                        } else if (
                          uploadResult.resultObject.success.toString() ==
                          "false"
                        ) {
                          msg = uploadResult.resultObject.msg;
                        }
                        DialogUtil.dialogWarn(msg);
                      }
                    }
                  );
                } else {
                  DialogUtil.dialogWarn(selectResult.msg);
                }
              }
            );
          }
        },
        {
          text: "拍照",
          onClick: function() {
            //拍照
            AppUtil.call(
              AppUtil.APIS.TAKE_PICTURE,
              {
                compress: { sizeLimit: "500", unit: "K" },
                format: "JPG",
                maxNum: "1"
              },
              function(selectResult) {
                if (selectResult.success.toString() == "true") {
                  var filePath = selectResult.resultObject;

                  //上传
                  common.loading("上传中");
                  AppUtil.call(
                    AppUtil.APIS.UPLOAD_PICTURE,
                    {
                      url: App.serverUrl + "/app/img/upload.do",
                      file: filePath,
                      data: {
                        shopId: pageVariables.shopId,
                        uploadType: target.attr("uploadType")
                      }
                    },
                    function(uploadResult) {
                      common.closeLoading();
                      if (
                        uploadResult.success.toString() == "true" &&
                        uploadResult.resultObject.success.toString() == "true"
                      ) {
                        target.attr(
                          "yzImgUrl",
                          uploadResult.resultObject.resultObject.imageUrl
                        );
                        target.attr(
                          "weixinImgUrl",
                          uploadResult.resultObject.resultObject
                            .thirdPartyImageUrl
                        );

                        //显示图片
                        target.find("img").attr("src", filePath);
                        displayPicture(target, "show");

                        listen4NextStep();
                      } else {
                        var msg = "";
                        if (uploadResult.success.toString() == "false") {
                          msg = uploadResult.msg;
                        } else if (
                          uploadResult.resultObject.success.toString() ==
                          "false"
                        ) {
                          msg = uploadResult.resultObject.msg;
                        }
                        DialogUtil.dialogWarn(msg);
                      }
                    }
                  );
                } else {
                  DialogUtil.dialogWarn(selectResult.msg);
                }
              }
            );
          }
        }
      ],
      [
        {
          text: "取消",
          color: "danger",
          onClick: function() {}
        }
      ]
    ]);
  }

  function displayPicture(target, showOrHide) {
    if (showOrHide == "show") {
      target.find("div.upload-before").hide();
      target.find("div.upload-after").show();
    } else if (showOrHide == "hide") {
      target.find("div.upload-before").show();
      target.find("div.upload-after").hide();
    }
  }

  /**
   * 测试接口
   * @type {Object}
   */
  var weixinOpenStep1Test = {
    testSendVerifyCode: function(options) {
      //发送验证码
      options.success({
        success: true
      });
    }
  };
})(document, window);
